package com._520it.crm.service.impl;

import com._520it.crm.domain.Client;
import com._520it.crm.domain.ClientReport;
import com._520it.crm.domain.Employee;
import com._520it.crm.domain.TransferRecord;
import com._520it.crm.mapper.ClientMapper;
import com._520it.crm.mapper.ClientReportMapper;
import com._520it.crm.mapper.EmployeeMapper;
import com._520it.crm.mapper.TransferRecordMapper;
import com._520it.crm.page.PageResult;
import com._520it.crm.query.QueryObject;
import com._520it.crm.service.ITransferRecordService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;
@Service
public class TransferRecordServiceImpl implements ITransferRecordService {
    @Autowired
    TransferRecordMapper transferRecordMapper;
    @Autowired
    ClientMapper clientMapper;
    @Autowired
    ClientReportMapper clientReportMapper;
    @Autowired
    EmployeeMapper employeeMapper;
    @Override
    public int deleteByPrimaryKey(Integer id) {
        transferRecordMapper.deleteByPrimaryKey(id);
        return 0;
    }



    @Override
    public PageResult query(QueryObject qo) {
        Integer totalCount = transferRecordMapper.queryforCount(qo);
        if (totalCount==0){
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        List<TransferRecord> data = transferRecordMapper.queryforList(qo);
        return new PageResult(totalCount,data);
    }
    //移交业务方法
    @Override
    public void insert(TransferRecord transferRecord ,Long clientId) {

        transferRecord.setTransferTime(new Date());
        clientMapper.transferById(clientId,transferRecord.getManagerId());
        transferRecord.getManagerId();
        Subject subject = SecurityUtils.getSubject();
        Employee current = (Employee) subject.getPrincipal();
        transferRecord.setCurrentName(current.getName());
        transferRecordMapper.insert(transferRecord);
        //-------------------------------报表业务-----------------------------------
        //报表的增加与更改业务
        //被移交客户的id(用来得到status)
        Client client = clientMapper.selectByPrimaryKey(clientId);


        //移交前的负责人id
        Long princiUserId = transferRecord.getPrinciUser();
        //移交前负责人的名字
        Employee princiUserEmployee = employeeMapper.selectById(princiUserId);
        String princiName = princiUserEmployee.getName();

        //移交后的负责人的id
        Long managerId = transferRecord.getManagerId();
        //移交后负责人的名字
        Employee managerEmployee = employeeMapper.selectById(managerId);
        String managerEmployeeName = managerEmployee.getName();
        Integer status = client.getStatus();


                        //-----------移交前的负责人操作-------------
        //先看下在报表里面有没有同一个移交前负责人的记录(员工id+客户状态确定唯一)
        //有的话就是更新,没有的话就插入一条
        ClientReport princiClientReport = clientReportMapper.selectByprinciUserAndstatus(princiUserId,status);

        //如果该记录存在,只需要更新他的数量就行了
        if (princiClientReport != null){


            //客户的状态
            if (status == Client.HIDDEN_CLIENT){
                //如果是为潜在客户,就得到该负责人潜在客户的数量
                Integer   hiddenNumber = princiClientReport.getHiddenNumber();
                if (hiddenNumber > 0){
                    hiddenNumber--;
                }
                princiClientReport.setHiddenNumber(hiddenNumber);
            }else if (status == Client.FORMAL_CLIENT){
                Integer  formalNumber = princiClientReport.getFormalNumber();
                 if (formalNumber >0){
                     formalNumber--;
                 }
                princiClientReport.setFormalNumber(formalNumber);
            }
            clientReportMapper.updateByPrimaryKey(princiClientReport);



            //else:如果clientReport不存在则添加一条
        }else{

            ClientReport newClientReport = new ClientReport();
            newClientReport.setPrinciUser(princiUserId);
            newClientReport.setName(princiName);
            newClientReport.setStatus(client.getStatus());
            if (status == Client.HIDDEN_CLIENT){
                newClientReport.setHiddenNumber(0);

            }else if(status == Client.FORMAL_CLIENT){
                newClientReport.setFormalNumber(0);
            }
           clientReportMapper.insert(newClientReport);

        }

                //-----------移交后的负责人操作-------------

        ClientReport managerClientReport = clientReportMapper.selectByprinciUserAndstatus(managerId,status);

        //如果该记录存在,只需要更新他的数量就行了
        if (managerClientReport != null){



            //客户的状态
            if (status == Client.HIDDEN_CLIENT){
                //如果是为潜在客户,就得到该负责人潜在客户的数量
                Integer hiddenNumber = managerClientReport.getHiddenNumber();

                    hiddenNumber++;

                managerClientReport.setHiddenNumber(hiddenNumber);

            }else if (status == Client.FORMAL_CLIENT){
                Integer  formalNumber = managerClientReport.getFormalNumber();

                    formalNumber++;

                managerClientReport.setFormalNumber(formalNumber);
            }
            clientReportMapper.updateByPrimaryKey(managerClientReport);


            //else:如果clientReport不存在则添加一条
        }else{
            ClientReport newClientReport1 = new ClientReport();
            newClientReport1.setPrinciUser(managerId);
            newClientReport1.setName(managerEmployeeName);
            newClientReport1.setStatus(client.getStatus());
            if (status == Client.HIDDEN_CLIENT){
                newClientReport1.setHiddenNumber(1);

            }else if(status == Client.FORMAL_CLIENT){
                newClientReport1.setFormalNumber(1);
            }
            clientReportMapper.insert(newClientReport1);
        }

    }

    @Override
    public void deleteall() {
        transferRecordMapper.deleteall();
    }
}
